package com.tg.push;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;

import static com.tg.push.BasePusher.HUAWEIPUSH_APPID;
import static com.tg.push.BasePusher.MI_PREFIX;
import static com.tg.push.BasePusher.MI_PUSH_APPID;
import static com.tg.push.BasePusher.MI_PUSH_APPKEY;
import static com.tg.push.BasePusher.OPPO_PREFIX;
import static com.tg.push.BasePusher.OPPO_PUSH_APPKEY;
import static com.tg.push.BasePusher.OPPO_PUSH_APPSECRET;
import static com.tg.push.BasePusher.VIVO_PUSH_APPID;
import static com.tg.push.BasePusher.VIVO_PUSH_APPKEY;

public class PushUtils {


    public static boolean isOppoEnable(Context context) {
        Bundle bundle = getApplicationBundle(context);
        return !isEmpty(bundle,OPPO_PUSH_APPKEY) && !isEmpty(bundle,OPPO_PUSH_APPSECRET);
    }
    private static boolean isEmpty( Bundle bundle ,String key){
        if (bundle==null)return true;
        Object object = bundle.get(key);
        if (object!=null&&object instanceof String){
          String v =   ((String) object).replace(OPPO_PREFIX,"").replace(MI_PREFIX,"");
          return TextUtils.isEmpty(v);
        }
        return object==null;
    }
    public static boolean isHuaweiEnable(Context context) {
        Bundle bundle = getApplicationBundle(context);
        return !isEmpty(bundle,HUAWEIPUSH_APPID);
    }
    public static boolean isVivoEnable(Context context) {

        Bundle bundle = getApplicationBundle(context);
        return !isEmpty(bundle,VIVO_PUSH_APPID) && !isEmpty(bundle,VIVO_PUSH_APPKEY);
    }
    public static boolean isMiEnable(Context context) {
        Bundle bundle = getApplicationBundle(context);
        return  !isEmpty(bundle,MI_PUSH_APPID) && !isEmpty(bundle,MI_PUSH_APPKEY);
    }
    public static Bundle getApplicationBundle(Context context) {
        try {
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            if (appInfo != null) {
                return appInfo.metaData;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }
    public static String getBundleValue(Context context,String key,String defaultValue){
        try {
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            if (appInfo != null) {
                String val = appInfo.metaData.getString(key, defaultValue).trim();
              return val;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return defaultValue;
    }
}
